#include <linux/input.h>

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#include <sys/ioctl.h>


/* 01_get_input_information /dev/input/event */
int main(int argc, char* argv[])
{

	int fd;
	int err;
	int Len;
	int bit;
	struct input_id id;
	unsigned char byte;
	unsigned int evbit[2];

	char* EV_NAMES[] = {
		"EV_SYN"		
		"EV_KEY"		
		"EV_REL"		
		"EV_ABS"		
		"EV_MSC"		
		"EV_SW"		
		"NULL"
		"NULL"
		"NULL"
		"NULL"
		"NULL"
		"EV_LED"	
 		"EV_SND"	
		"NULL"
		"EV_REP"		
		"EV_FF"		
		"EV_PWR"			
		"EV_FF_STATUS"	
 		"EV_MAX"	
 		"EV_CNT"	
	}
	
	/* 参数不对,打印用法 */
	if(argc != 2)
	{
		printf("Usage:./%s /dev/input/event", argv[0]);
		return -1;
	}
 
	fd = open(argv[1], O_RDWR);
	if(fd < 0)
	{
		printf("open %s error", argv[1]);
		return -1;
	}
	
	err = ioctl(fd, EVIOCGID, &id);
	if(err == 0)
	{
		printf("bustype = 0x%x\n", id.bustype);
		printf("vendor = 0x%x\n", id.vendor);
		printf("product = 0x%x\n", id.product);
		printf("version = 0x%x\n", id.version);
	}

	Len = ioctl(fd, EVIOCGBIT(0, sizeof(evbit)), &evbit);
 	if(Len > 0 && Len <= sizeof(evbit))
	{
		printf("support ev type: ");
		for(i = 0; i < Len; i++)
		{
			byte = ((unsigned char*)evbit)[i];
			for(bit = 0; bit < 8; bit++)
			{
				if(byte & (1<<bit))
				{
					printf("%s ",ev_names[i*8 + bit]);
				}
			}
		}
		printf("\n");
	}
	
}

